/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.financial.web.declarationnotice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.core.util.StringCollectionUtil;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.customsdeclarationinfo.CustomsDeclarationInfo;
import com.jeesite.modules.base.entity.customsdeclarationinfo.CustomsDeclarationInfoDetail;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.base.service.customsdeclarationinfo.CustomsDeclarationInfoService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.custom.enumInfo.warehourseEnum;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.logistics.entity.customsdeclaration.CustomsDeclaration;
import com.jeesite.modules.logistics.entity.ladingbill.LadingBill;
import com.jeesite.modules.logistics.service.customsdeclaration.CustomsDeclarationService;
import com.jeesite.modules.logistics.service.ladingbill.LadingBillService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.UserService;
import com.twelvemonkeys.lang.DateUtil;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.financial.entity.declarationnotice.TdeclarationNotice;
import com.jeesite.modules.financial.service.declarationnotice.TdeclarationNoticeService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 报关通知Controller
 * @author shengbin
 * @version 2021-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/financial/declarationnotice/tdeclarationNotice")
public class TdeclarationNoticeController extends BaseController {

	@Autowired
	private TdeclarationNoticeService tdeclarationNoticeService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private ServiceOrderService serviceOrderService;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private UserService userService;
	@Autowired
	private TcarService tcarService;
	@Autowired
	private LadingBillService ladingBillService;
	@Autowired
	private CustomsDeclarationInfoService customsDeclarationInfoService;
	@Autowired
	private CustomsDeclarationService customsDeclarationService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TdeclarationNotice get(String customsDeclarationNoticeId, boolean isNewRecord) {
		return tdeclarationNoticeService.get(customsDeclarationNoticeId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("financial:declarationnotice:tdeclarationNotice:view")
	@RequestMapping(value = {"list", ""})
	public String list(TdeclarationNotice tdeclarationNotice, Model model) {

		List<Customer> listCustomer= customerService.findList(new Customer());
		ServiceOrder serviceOrder=new ServiceOrder();
		List<ServiceOrder> listOrder=serviceOrderService.findList(serviceOrder);
		Supplier supplier=new Supplier();
		supplier.setSupplierType(warehourseEnum.BAOGUANHANG.getCode());//报关行
		List<Supplier> listSupplier=supplierService.findList(supplier);

		List<Tcar> listCar=tcarService.findList(new Tcar());

		List<LadingBill> listLadingbill=ladingBillService.findList(new LadingBill());

		model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("listOrder", listOrder);
		model.addAttribute("listSupplier", listSupplier);
		model.addAttribute("listCustomer", listCustomer);
		model.addAttribute("listCar", listCar);
		model.addAttribute("listLadingbill", listLadingbill);
		model.addAttribute("tdeclarationNotice", tdeclarationNotice);
		return "modules/financial/declarationnotice/tdeclarationNoticeList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("financial:declarationnotice:tdeclarationNotice:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TdeclarationNotice> listData(TdeclarationNotice tdeclarationNotice, HttpServletRequest request, HttpServletResponse response) {
		tdeclarationNotice.setPage(new Page<>(request, response));
		Page<TdeclarationNotice> page = tdeclarationNoticeService.findPage(tdeclarationNotice);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("financial:declarationnotice:tdeclarationNotice:view")
	@RequestMapping(value = "form")
	public String form(TdeclarationNotice tdeclarationNotice, Model model) {

		List<Customer> listCustomer= customerService.findList(new Customer());
		ServiceOrder serviceOrder=new ServiceOrder();
		List<ServiceOrder> listOrder=serviceOrderService.findList(serviceOrder);
		Supplier supplier=new Supplier();
		supplier.setSupplierType(warehourseEnum.BAOGUANHANG.getCode());//报关行
		List<Supplier> listSupplier=supplierService.findList(supplier);

		List<Tcar> listCar=tcarService.findList(new Tcar());

		List<LadingBill> listLadingbill=ladingBillService.findList(new LadingBill());

		model.addAttribute("tdeclarationNotice", tdeclarationNotice);
		model.addAttribute("listOrder", listOrder);
		model.addAttribute("listSupplier", listSupplier);
		model.addAttribute("listCustomer", listCustomer);
		model.addAttribute("listCar", listCar);
		model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("listLadingbill", listLadingbill);
		return "modules/financial/declarationnotice/tdeclarationNoticeForm";
	}
	@RequestMapping(value = "getServiceOrderById")
	public ServiceOrder getServiceOrderById(String serviceOrderId) {
		ServiceOrder serviceOrder = serviceOrderService.get(serviceOrderId);
		return serviceOrder;
	}
	@RequestMapping(value = "notice")
	public void notice(String customsDeclarationNoticeId) {
		TdeclarationNotice tdeclarationNotice = tdeclarationNoticeService.get(customsDeclarationNoticeId);
		tdeclarationNotice.setDate(new Date());
		tdeclarationNotice.setIsNotice("1");
		tdeclarationNoticeService.update(tdeclarationNotice);
	}

	@RequestMapping(value = "getNoticeList")
	public List<CustomsDeclarationInfoDetail> getNoticeList(String toPort, String [] areas) {

		List<CustomsDeclarationInfoDetail> list=new ArrayList<>();
		for (int i=0;i<areas.length;i++) {
			CustomsDeclarationInfo customsDeclarationInfo = new CustomsDeclarationInfo();
			customsDeclarationInfo.setDestination(toPort);
			customsDeclarationInfo.setVersionType(areas[i]);
			List<CustomsDeclarationInfo> listCus= customsDeclarationInfoService.findList(customsDeclarationInfo);

			if(listCus.size()>0){
				for(CustomsDeclarationInfo cusInfo:listCus){
					cusInfo=customsDeclarationInfoService.get(cusInfo);
					if (cusInfo.getCustomsDeclarationInfoDetailList().size() > 0) {
						for (CustomsDeclarationInfoDetail customsDeclarationInfoDetail : cusInfo.getCustomsDeclarationInfoDetailList()) {
							list.add(customsDeclarationInfoDetail);
						}
					}
				}


			}

		}
		return list;
	}


	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("financial:declarationnotice:tdeclarationNotice:view")
	@RequestMapping(value = "detail")
	public String detail(TdeclarationNotice tdeclarationNotice, Model model) {
		List<Customer> listCustomer= customerService.findList(new Customer());
		ServiceOrder serviceOrder=new ServiceOrder();
		List<ServiceOrder> listOrder=serviceOrderService.findList(serviceOrder);
		Supplier supplier=new Supplier();
		supplier.setSupplierType(warehourseEnum.BAOGUANHANG.getCode());//报关行
		List<Supplier> listSupplier=supplierService.findList(supplier);

		List<Tcar> listCar=tcarService.findList(new Tcar());

		List<LadingBill> listLadingbill=ladingBillService.findList(new LadingBill());

		List<CustomsDeclaration> customsDeclarationList=customsDeclarationService.findList(new CustomsDeclaration());

		model.addAttribute("tdeclarationNotice", tdeclarationNotice);
		model.addAttribute("listOrder", listOrder);
		model.addAttribute("listSupplier", listSupplier);
		model.addAttribute("listCustomer", listCustomer);
		model.addAttribute("listCar", listCar);
		model.addAttribute("listLadingbill", listLadingbill);
		model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("customsDeclarationList", customsDeclarationList);
		return "modules/financial/declarationnotice/tdeclarationNoticeDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("financial:declarationnotice:tdeclarationNotice:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TdeclarationNotice tdeclarationNotice) {
		tdeclarationNoticeService.save(tdeclarationNotice);
		return renderResult(Global.TRUE, text("保存报关通知成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("financial:declarationnotice:tdeclarationNotice:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TdeclarationNotice tdeclarationNotice) {
		tdeclarationNotice.setStatus(TdeclarationNotice.STATUS_DISABLE);
		tdeclarationNoticeService.updateStatus(tdeclarationNotice);
		return renderResult(Global.TRUE, text("停用报关通知成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("financial:declarationnotice:tdeclarationNotice:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TdeclarationNotice tdeclarationNotice) {
		tdeclarationNotice.setStatus(TdeclarationNotice.STATUS_NORMAL);
		tdeclarationNoticeService.updateStatus(tdeclarationNotice);
		return renderResult(Global.TRUE, text("启用报关通知成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("financial:declarationnotice:tdeclarationNotice:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TdeclarationNotice tdeclarationNotice) {
		tdeclarationNoticeService.delete(tdeclarationNotice);
		return renderResult(Global.TRUE, text("删除报关通知成功！"));
	}
	
}